//
//  WSListViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSListViewController.h"
#import "WSResource.h"
#import "WSTopListModel.h"
#import "WSFocusModel.h"
#import "WSChildListModel.h"
#import "WSListCell.h"
#import "WSLunboView.h"
#import "WSListHeadView.h"
#import "WHListDetailViewController.h"

@interface WSListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,retain)WSLunboView *headView;
@property(nonatomic,retain)NSMutableArray *titleArray;
@property(nonatomic,retain)NSMutableArray *topListArray;
@property(nonatomic,retain)NSMutableArray *imageArray;

@end

@implementation WSListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
    [self getData];
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[WSListCell class] forCellReuseIdentifier:@"listTableCell"];
    [self.tableView reloadData];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    self.tableView.sectionFooterHeight = 10;
    
    self.headView = [[WSLunboView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*0.45)];
    self.tableView.tableHeaderView = self.headView;

//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
//    self.listTableView.header = header;
//    [self.listTableView.header beginRefreshing];
    
    
}

#pragma mark - tableView delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.titleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableArray *array = [self.topListArray objectAtIndex:section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WSListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listTableCell"];
    
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];

    WSTopListModel *model = [array objectAtIndex:indexPath.row];
    if (indexPath.row < array.count - 1) {
        cell.isShowLine = YES;
    }
    cell.topModel = model;
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    WSListHeadView *headView = [[WSListHeadView alloc] init];
    headView.title = [self.titleArray objectAtIndex:section];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return SCREEN_WIDTH * 0.26;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];
    
    WSTopListModel *model = [array objectAtIndex:indexPath.row];
    
    WHListDetailViewController *listDetailVC = [[WHListDetailViewController alloc] init];
    listDetailVC.key = model.key;
    listDetailVC.contentType = model.contentType;
    listDetailVC.navTitle = model.title;
    [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:listDetailVC];
    [self.navigationController pushViewController:listDetailVC animated:YES];
    
}

#pragma mark - 请求数据
- (void)getData {
    
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios&device=iphone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2";
    [WSResource getDataWithURL:urlStr block:^(id result) {
        
        //初始化数组
        self.topListArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
        NSMutableArray *dataArray = [result objectForKey:@"datas"];
        
        for (NSMutableDictionary *dic in dataArray) {
            
            [self.titleArray addObject:[dic objectForKey:@"title"]];
            
            NSMutableArray *listArray = [dic objectForKey:@"list"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSMutableDictionary *topDic in listArray) {
                
                WSTopListModel *model = [WSTopListModel modelWithDictionary:topDic];
                [array addObject:model];
            }
            [self.topListArray addObject:array];
        }
        
        //轮播图数据
        WSFocusModel *focusModel = [WSFocusModel modelWithDictionary:[result objectForKey:@"focusImages"]];
        [self.headView createSubViews:focusModel.list];
        
        [self.tableView reloadData];
//        [self.listTableView.header endRefreshing];
    } error:^(NSError *error) {
        
//        [self.listTableView.header endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
